import { NextResponse } from "next/server";
import fs from "fs/promises";
import path from "path";

const infoPath = path.join(process.cwd(), "src/config/info.json");
let cache = null;
let lastMtime = null;

async function loadCacheIfNeeded() {
  const stat = await fs.stat(infoPath);
  if (!cache || !lastMtime || stat.mtimeMs !== lastMtime) {
    const data = await fs.readFile(infoPath, "utf-8");
    cache = JSON.parse(data);
    lastMtime = stat.mtimeMs;
  }
}

export async function GET() {
  try {
    await loadCacheIfNeeded();
    return NextResponse.json({ code: 0, data: cache });
  } catch (err) {
    return NextResponse.json({ code: 1, message: "Reading failed", error: err.message }, { status: 500 });
  }
}